import json

import requests
from jsonpath import jsonpath


class LG(object):
    def __init__(self):
        self.url = 'https://www.lagou.com/lbs/getAllCitySearchLabels.json'
        self.header = {
            'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
        }

    @staticmethod
    def todict(string):
        return json.loads(string)

    # 获取所有城市名
    @staticmethod
    def city_name(city_dict):
        city = jsonpath(city_dict, "$..name")
        print("city_name:", city)

    # 获取某个节点下的所有城市名
    @staticmethod
    def node_city_name(city_dict):
        city = jsonpath(city_dict, "$..A[*].name")
        print("city_name:", city)

    def send(self):
        res = requests.get(self.url, headers=self.header)
        dic = self.todict(res.text)
        # self.city_name(dic)
        self.node_city_name(dic)


if __name__ == "__main__":
    st = LG()
    st.send()
